<?php

namespace app\partner\controller;

use app\common\controller\CommonService;
use think\Controller;
use think\Db;
use think\Request;

class Orders extends Controller
{

    /**
     * 淘宝订单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tbOrder(Request $request)
    {
        $partnerId = $request->partnerId;
        $requestData = $request->post();
        $uids = Db::name('user_manage')->where('partner_id', $partnerId)->column('id');
        if (empty($uids)) return CommonService::success([]);

        $db = Db::name('tb_order')
            ->join('user_manage user', 'tb_order.uid = user.id')
            ->leftJoin('user_manage parUser', 'user.par_id = parUser.id')
            ->where('tb_order.uid','in', $uids);

        if (!empty($requestData['order_id'])) {
            $db = $db->where('tb_order.order_id', $requestData['order_id']);
        }
        if (!empty($requestData['start_order_time']) && !empty($requestData['end_order_time'])) {
            $db = $db->whereBetween('tb_order.order_time', [$requestData['start_order_time'], $requestData['end_order_time']]);
        }
        if (isset($requestData['order_status']) && strlen($requestData['order_status']) > 0) {
            $db = $db->where('tb_order.order_status', $requestData['order_status']);
        }
        if (!empty($requestData['start_order_ok_time']) && !empty($requestData['end_order_ok_time'])) {
            $db = $db->whereBetween('tb_order.order_ok_time', [$requestData['start_order_ok_time'], $requestData['end_order_ok_time']]);
        }
        if (!empty($requestData['user_code'])) {
            $db = $db->where('user.code', $requestData['user_code']);
        }
        if (!empty($requestData['rebate_status'])) {
            switch ($requestData['rebate_status']) {
                case 1;//未返利
                    $db = $db->where('tb_order.is_rebate', 0)->where('is_weiquan', 0)->where('order_status', '<>', 0);
                    break;
                case 2;//已返利
                    $db = $db->where('tb_order.is_rebate', 1)->where('is_weiquan', 0)->where('order_status', '<>', 0);
                    break;
                case 3;//不返利
                    $db = $db->where('tb_order.is_rebate', 0)->where('is_weiquan', 0)->where('order_status', 0);
                    break;
                case 4;//已扣除
                    $db = $db->where('tb_order.is_rebate', 1)->where('is_tuihuo_rebate', 1);
                    break;
            }
        }
        if (!empty($requestData['start_rebate_time']) && !empty($requestData['end_rebate_time'])) {
            $db = $db->whereBetween('tb_order.update_time', [$requestData['start_rebate_time'], $requestData['end_rebate_time']]);
            $db = $db->where('tb_order.is_rebate',1);
        }
        if (!empty($requestData['par_user_code'])) {
            $db = $db->where('parUser.code', $requestData['par_user_code']);
        }
        if (in_array(input('orderBy'), ['order_time', 'user_buy_price', 'order_ok_time', 'rebate_fee', 'update_time','partner_rebate_fee']) && in_array(input('sort'), ['asc', 'desc'])) {
            $orderBy = input('orderBy');
            $sort = input('sort');
        } else {
            $orderBy = 'tb_order.order_time';
            $sort = 'desc';
        }
        $db1 = clone $db;
        $data = $db->page(input('page', 1), input('listRows', 20))->order($orderBy, $sort)
            ->field('is_weiquan,is_tuihuo_rebate,order_id,order_time,order_type,goods_price,user_buy_price,order_status,order_ok_time,rebate_fee,user.nickname as user_nickname,user.code as user_code,user.is_vip as user_is_vip,user_rebate_fee,is_rebate,update_time,parUser.nickname as par_user_nickname,parUser.code as par_user_code,par_user_rebate_fee,partner_rebate_fee')
            ->select();

        foreach ($data as $k => $v) {
            $data[$k]['user_nickname'] = CommonService::emoji_decode($v['user_nickname']);
            if ($data[$k]['user_is_vip'] == 1 || $data[$k]['user_is_vip'] > time()) {
                $data[$k]['user_is_vip'] = '是';
            } else {
                $data[$k]['user_is_vip'] = '否';
            }
            if (!empty($data[$k]['user_rebate_time'])) {
                $data[$k]['user_rebate_time'] = date('Y-m-d H:i:s', $data[$k]['user_rebate_time']);
            }
            $data[$k]['par_user_nickname'] = CommonService::emoji_decode($v['par_user_nickname']);
            if ($data[$k]['is_weiquan'] == 1) {
                $data[$k]['order_status'] = 0;
            }

            if ($data[$k]['is_rebate'] == 1){
                $data[$k]['user_rebate_time'] = $v['update_time'];
            }
            if ($data[$k]['is_rebate'] == 1) {
                $data[$k]['rebate_status'] = '已返利';
                if ($data[$k]['is_tuihuo_rebate'] == 1) {
                    $data[$k]['rebate_status'] = '已扣除';
                }
            } else {
                $data[$k]['rebate_status'] = '待返利';
                if ($data[$k]['order_status'] == 0) {
                    $data[$k]['rebate_status'] = '不返利';
                }
            }
            switch ($data[$k]['order_type']){
                case 1;
                    $data[$k]['order_type'] = '普通';
                    break;
                case 2;
                    $data[$k]['order_type'] = '零元购';
                    break;
                case 3;
                    $data[$k]['order_type'] = '积分商城';
                    break;
            }
            switch ($data[$k]['order_status']) {
                case 0:
                    $data[$k]['order_status'] = '已失效';
                    break;
                case 1:
                    $data[$k]['order_status'] = '已付款';
                    break;
                case 2:
                    $data[$k]['order_status'] = '已收货';
                    break;
                case 3:
                    $data[$k]['order_status'] = '已结算';
                    break;
            }
            unset($data[$k]['is_weiquan'], $data[$k]['is_tuihuo_rebate'], $data[$k]['is_rebate']);
        }
        $res['list'] = $data;
        $res['total'] = $db1->count();
        return CommonService::success($res);
    }


    /**
     * 京东订单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function jdOrder(Request $request)
    {
        $partnerId = $request->partnerId;
        $requestData = $request->post();
        $uids = Db::name('user_manage')->where('partner_id', $partnerId)->column('id');
        if (empty($uids)) return CommonService::success([]);

        $db = Db::name('jd_order')
            ->join('user_manage user', 'jd_order.uid = user.id')
            ->leftJoin('user_manage parUser', 'user.par_id = parUser.id')
            ->where('jd_order.uid','in', $uids);
        if (!empty($requestData['order_id'])) {
            $db = $db->where('jd_order.order_id', $requestData['order_id']);
        }
        if (!empty($requestData['start_order_time']) && !empty($requestData['end_order_time'])) {
            $db = $db->whereBetween('jd_order.order_time', [strtotime($requestData['start_order_time']) * 1000, strtotime($requestData['end_order_time']) * 1000]);
        }
        if (isset($requestData['order_status']) && strlen($requestData['order_status']) > 0) {
            $db = $db->where('jd_order.order_status', $requestData['order_status']);
        }
        if (!empty($requestData['start_order_ok_time']) && !empty($requestData['end_order_ok_time'])) {
            $db = $db->whereBetween('jd_order.order_ok_time', [strtotime($requestData['start_order_ok_time']) * 1000, strtotime($requestData['end_order_ok_time']) * 1000]);
        }
        if (!empty($requestData['user_code'])) {
            $db = $db->where('user.code', $requestData['user_code']);
        }
        if (!empty($requestData['rebate_status'])) {
            switch ($requestData['rebate_status']) {
                case 1;//未返利
                    $db = $db->where('jd_order.is_rebate', 0)->where('order_status', '<>', 0);
                    break;
                case 2;//已返利
                    $db = $db->where('jd_order.is_rebate', 1)->where('order_status', '<>', 0);
                    break;
                case 3;//不返利
                    $db = $db->where('jd_order.is_rebate', 0)->where('order_status', 0);
                    break;
                case 4;//已扣除
                    $db = $db->where('jd_order.is_rebate', 1)->where('is_tuihuo_rebate', 1);
                    break;
            }
        }
        if (!empty($requestData['start_rebate_time']) && !empty($requestData['end_rebate_time'])) {
            $db = $db->whereBetween('jd_order.update_time', [$requestData['start_rebate_time'], $requestData['end_rebate_time']]);
            $db = $db->where('jd_order.is_rebate',1);
        }
        if (!empty($requestData['par_user_code'])) {
            $db = $db->where('parUser.code', $requestData['par_user_code']);
        }
        if (in_array(input('orderBy'), ['order_time', 'user_buy_price', 'order_ok_time', 'rebate_fee', 'update_time','partner_rebate_fee']) && in_array(input('sort'), ['asc', 'desc'])) {
            $orderBy = input('orderBy');
            $sort = input('sort');
        } else {
            $orderBy = 'jd_order.order_time';
            $sort = 'desc';
        }
        $db1 = clone $db;
        $data = $db->page(input('page', 1), input('listRows', 20))->order($orderBy, $sort)
            ->field('is_tuihuo_rebate,order_id,order_time,goods_price,user_buy_price,order_status,order_ok_time,rebate_fee,user.nickname as user_nickname,user.code as user_code,user.is_vip as user_is_vip,user_rebate_fee,is_rebate,update_time,parUser.nickname as par_user_nickname,parUser.code as par_user_code,par_user_rebate_fee,partner_rebate_fee')
            ->select();

        foreach ($data as $k => $v) {
            $data[$k]['order_time'] = date('Y-m-d H:i:s', $data[$k]['order_time'] / 1000);
            if ($data[$k]['order_ok_time'] > 0) {
                $data[$k]['order_ok_time'] = date('Y-m-d H:i:s', $data[$k]['order_ok_time'] / 1000);
            } else {
                $data[$k]['order_ok_time'] = '';
            }


            $data[$k]['user_nickname'] = CommonService::emoji_decode($v['user_nickname']);
            if ($data[$k]['user_is_vip'] == 1 || $data[$k]['user_is_vip'] > time()) {
                $data[$k]['user_is_vip'] = '是';
            } else {
                $data[$k]['user_is_vip'] = '否';
            }
            if (!empty($data[$k]['user_rebate_time'])) {
                $data[$k]['user_rebate_time'] = date('Y-m-d H:i:s', $data[$k]['user_rebate_time']);
            }
            $data[$k]['par_user_nickname'] = CommonService::emoji_decode($v['par_user_nickname']);

            if ($data[$k]['is_rebate'] == 1){
                $data[$k]['user_rebate_time'] = $v['update_time'];
            }
            if ($data[$k]['is_rebate'] == 1) {
                $data[$k]['rebate_status'] = '已返利';
                if ($data[$k]['is_tuihuo_rebate'] == 1) {
                    $data[$k]['rebate_status'] = '已扣除';
                }
            } else {
                $data[$k]['rebate_status'] = '待返利';
                if ($data[$k]['order_status'] == 0) {
                    $data[$k]['rebate_status'] = '不返利';
                }
            }
            switch ($data[$k]['order_status']) {
                case 0:
                    $data[$k]['order_status'] = '已失效';
                    break;
                case 1:
                    $data[$k]['order_status'] = '已付款';
                    break;
                case 2:
                    $data[$k]['order_status'] = '已收货';
                    break;
                case 3:
                    $data[$k]['order_status'] = '已结算';
                    break;
            }
            unset($data[$k]['is_tuihuo_rebate'], $data[$k]['is_rebate']);
        }
        $res['list'] = $data;
        $res['total'] = $db1->count();
        return CommonService::success($res);
    }

    /**
     * 拼多多订单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function pddOrder(Request $request)
    {
        $partnerId = $request->partnerId;
        $requestData = $request->post();
        $uids = Db::name('user_manage')->where('partner_id', $partnerId)->column('id');
        if (empty($uids)) return CommonService::success([]);

        $db = Db::name('pdd_order')
            ->join('user_manage user', 'pdd_order.uid = user.id')
            ->leftJoin('user_manage parUser', 'user.par_id = parUser.id')
            ->where('pdd_order.uid','in', $uids);
        if (!empty($requestData['order_id'])) {
            $db = $db->where('pdd_order.order_id', $requestData['order_id']);
        }
        if (!empty($requestData['start_order_time']) && !empty($requestData['end_order_time'])) {
            $db = $db->whereBetween('pdd_order.order_time', [strtotime($requestData['start_order_time']), strtotime($requestData['end_order_time'])]);
        }
        if (isset($requestData['order_status']) && strlen($requestData['order_status']) > 0) {
            $db = $db->where('pdd_order.order_status', $requestData['order_status']);
        }
        if (!empty($requestData['start_order_ok_time']) && !empty($requestData['end_order_ok_time'])) {
            $db = $db->whereBetween('pdd_order.order_ok_time', [strtotime($requestData['start_order_ok_time']), strtotime($requestData['end_order_ok_time'])]);
        }
        if (!empty($requestData['user_code'])) {
            $db = $db->where('user.code', $requestData['user_code']);
        }
        if (!empty($requestData['rebate_status'])) {
            switch ($requestData['rebate_status']) {
                case 1;//未返利
                    $db = $db->where('pdd_order.is_rebate', 0)->where('order_status', '<>', 0);
                    break;
                case 2;//已返利
                    $db = $db->where('pdd_order.is_rebate', 1)->where('order_status', '<>', 0);
                    break;
                case 3;//不返利
                    $db = $db->where('pdd_order.is_rebate', 0)->where('order_status', 0);
                    break;
                case 4;//已扣除
                    $db = $db->where('pdd_order.is_rebate', 1)->where('is_tuihuo_rebate', 1);
                    break;
            }
        }
        if (!empty($requestData['start_rebate_time']) && !empty($requestData['end_rebate_time'])) {
            $db = $db->whereBetween('pdd_order.update_time', [$requestData['start_rebate_time'], $requestData['end_rebate_time']]);
            $db = $db->where('pdd_order.is_rebate',1);
        }
        if (!empty($requestData['par_user_code'])) {
            $db = $db->where('parUser.code', $requestData['par_user_code']);
        }
        if (in_array(input('orderBy'), ['order_time', 'user_buy_price', 'order_ok_time', 'rebate_fee', 'update_time','partner_rebate_fee']) && in_array(input('sort'), ['asc', 'desc'])) {
            $orderBy = input('orderBy');
            $sort = input('sort');
        } else {
            $orderBy = 'pdd_order.order_time';
            $sort = 'desc';
        }
        $db1 = clone $db;
        $data = $db->page(input('page', 1), input('listRows', 20))->order($orderBy, $sort)
            ->field('is_tuihuo_rebate,order_id,order_time,goods_price,user_buy_price,order_status,order_ok_time,rebate_fee,user.nickname as user_nickname,user.code as user_code,user.is_vip as user_is_vip,user_rebate_fee,is_rebate,update_time,parUser.nickname as par_user_nickname,parUser.code as par_user_code,par_user_rebate_fee,partner_rebate_fee')
            ->select();

        foreach ($data as $k => $v) {
            $data[$k]['order_time'] = date('Y-m-d H:i:s', $data[$k]['order_time']);
            if ($data[$k]['order_ok_time'] > 0) {
                $data[$k]['order_ok_time'] = date('Y-m-d H:i:s', $data[$k]['order_ok_time']);
            } else {
                $data[$k]['order_ok_time'] = '';
            }


            $data[$k]['user_nickname'] = CommonService::emoji_decode($v['user_nickname']);
            if ($data[$k]['user_is_vip'] == 1 || $data[$k]['user_is_vip'] > time()) {
                $data[$k]['user_is_vip'] = '是';
            } else {
                $data[$k]['user_is_vip'] = '否';
            }
            if (!empty($data[$k]['user_rebate_time'])) {
                $data[$k]['user_rebate_time'] = date('Y-m-d H:i:s', $data[$k]['user_rebate_time']);
            }
            $data[$k]['par_user_nickname'] = CommonService::emoji_decode($v['par_user_nickname']);

            if ($data[$k]['is_rebate'] == 1){
                $data[$k]['user_rebate_time'] = $v['update_time'];
            }
            if ($data[$k]['is_rebate'] == 1) {
                $data[$k]['rebate_status'] = '已返利';
                if ($data[$k]['is_tuihuo_rebate'] == 1) {
                    $data[$k]['rebate_status'] = '已扣除';
                }
            } else {
                $data[$k]['rebate_status'] = '待返利';
                if ($data[$k]['order_status'] == 0) {
                    $data[$k]['rebate_status'] = '不返利';
                }
            }
            switch ($data[$k]['order_status']) {
                case 0:
                    $data[$k]['order_status'] = '已失效';
                    break;
                case 1:
                    $data[$k]['order_status'] = '已付款';
                    break;
                case 2:
                    $data[$k]['order_status'] = '已收货';
                    break;
                case 3:
                    $data[$k]['order_status'] = '已结算';
                    break;
            }
            unset($data[$k]['is_tuihuo_rebate'], $data[$k]['is_rebate']);
        }
        $res['list'] = $data;
        $res['total'] = $db1->count();
        return CommonService::success($res);
    }


}
